package com.aiyige.page.publish.photo.adapter;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.publish.photo.model.PublishPhotoItem;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.KeyboardUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuyakun on 2017/11/9.
 */

public class PublishPhotoAdapter extends BaseQuickAdapter<PublishPhotoItem, PublishPhotoAdapter.ViewHolder> {
    @BindView(R.id.photoIv)
    ImageView photoIv;
    @BindView(R.id.deleteBtn)
    ImageView deleteBtn;
    @BindView(R.id.photoLayout)
    FrameLayout photoLayout;
    @BindView(R.id.introductionEt)
    EditText introductionEt;
    @BindView(R.id.expandableLayout)
    ExpandableLayout expandableLayout;

    public PublishPhotoAdapter() {
        super(R.layout.publish_photo_item, new LinkedList<PublishPhotoItem>());
    }

    @Override
    protected void convert(ViewHolder helper, PublishPhotoItem item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.photoIv)
        ImageView photoIv;
        @BindView(R.id.deleteBtn)
        ImageView deleteBtn;
        @BindView(R.id.photoLayout)
        FrameLayout photoLayout;
        @BindView(R.id.introductionEt)
        EditText introductionEt;
        @BindView(R.id.expandableLayout)
        ExpandableLayout expandableLayout;
        PublishPhotoItem data;
        InputNumFilter inputNumFilter;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            addOnClickListener(R.id.deleteBtn);
            introductionEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    data.setIntroduction(s.toString().trim());
                }
            });
            inputNumFilter = new InputNumFilter(50, false);
            introductionEt.setFilters(new InputFilter[]{inputNumFilter});
            introductionEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
                }
            });
        }

        public void bindData(PublishPhotoItem item) {
            data = item;
            if (data.isExpand()) {
                expandableLayout.expand(false);
            } else {
                expandableLayout.collapse(false);
            }
            introductionEt.setText(data.getIntroduction());
            GlideUtil.with(itemView.getContext())
                    .asGif()
                    .loadCover(data.getPhotoUrl())
                    .into(photoIv);
        }

        @OnClick({R.id.photoLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.photoLayout:
                    data.setExpand(true);
                    expandableLayout.expand(false);
                    KeyboardUtil.showKeyboard(itemView.getContext(), introductionEt);
                    break;
            }
        }
    }

}
